<?php
/**
*   +----------------------------------------------------------------------
*   | Author: ONLY <491518132@qq.com>
*	+----------------------------------------------------------------------
*   | Creater Time : 2013-4-3
*   +----------------------------------------------------------------------
*   | Link ( http://www.goshopx.com  http://www.shopingj.com )
*   +----------------------------------------------------------------------
**/
class SuppliersAction extends BaseAction {
	protected $newpro;
	function _initialize() {
		parent::_initialize ();
		$this->dao = M ( 'Product_suppliers' );
		import ( 'Com.Newpro' );
		$this->newpro = new Newpro ();
	}
	/**
	 * (non-PHPdoc)
	 * 
	 * @see BaseAction::index()
	 */
	function index() {
		parent::index ( array (), $this->dao );
	}
	
	function suppliers() {
		$proids = explode ( ',', trim ( $_REQUEST ['proids'], ',' ) );
		$suppl = $_REQUEST ['suppl'];
		$model = M ( 'ProductToSuppliers' );
		$date = array ();
		$reid = 0;
		foreach ( $proids as $k => $v ) {
			foreach ( $suppl as $key => $value ) {
				$date ['product_id'] = $v;
				$date ['suppliers_id'] = $value;
				$reid = $model->add ( $date, array (), true );
				// echo $model->getLastSql();
			}
		}
		if ($reid === FALSE) {
			$this->_ajm ( 300, '添加失败！，请重试' );
		} else {
			$this->_ajm ( 200, '添加成功', 1, U ( 'Product/index' ) );
		}
	}
	
	/**
	 * 给供应商添加产品
	 */
	function addpro() {
		$proids = implode ( ',', $_REQUEST ['ids'] );
		$suppliers = $this->newpro->suppliers ( true );
		$this->assign ( 'suppliers', $suppliers );
		$this->assign ( 'proids', $proids );
		$this->display ();
	}
	
	/**
	 * 供应下的产品列表
	 */
	function supptopro() {
		$proids = $this->newpro->supptopro ( $_REQUEST ['suppid'] );
		$count=count($proids);
		$this->assign("totalCount",$count);
		if($count>0){
			$page=new Page($count,$this->listrows);
			$proids = array_slice($proids,$page->firstRow,$page->listRows);
			$proinfo = $this->newpro->protobaseinfo ( $proids );
			$this->assign ( 'suppid', $_REQUEST ['suppid'] );
			$this->assign ( 'list', $proinfo );
		}
		
		$this->display ( 'product' );
	}
	
	/**
	 * 删除供应商下的产品
	 */
	function delpro() {
		$proid = $_REQUEST ['proid'];
		$proids = $_REQUEST ['proids'];
		$suppid = $_REQUEST ['suppid'];
		$model = M ( 'ProductToSuppliers' );
		$where = array ();
		$reid = TRUE;
		if ($proid) {
			$reid = $model->where ( array (
					'product_id' => $proid,
					'suppliers_id' => $suppid 
			) )->delete ();
		} elseif ($proids) {
			foreach ( $proids as $k => $v ) {
				$where ['product_id'] = $v;
				$where ['suppliers_id'] = $suppid;
				$reid = $model->where ( $where )->delete ();
			}
		}
		if ($reid === FALSE) {
			$this->_ajm ( 300, '操作错误，请重试' );
		} else {
			$this->_ajm ( 200, '删除成功', 1, $url ? $url : U ( MODULE_NAME . '/index' ) );
		}
	}
	
	/**
	 * 插入前添加时间
	 */
	function _before_insert() {
		$_POST ['create_date'] = $_POST ['write_date'] = time ();
	}
	
	/**
	 * 更新前添加时间
	 */
	function _before_update() {
		$_POST ['create_date'] = time ();
	}
	
	/**
	 * 插入后更新缓存
	 */
	function _after_insert() {
		parent::_cache ( 1, 'suppliers' );
	}
	/**
	 * 更新
	 */
	function update() {
		parent::update ( $this->dao, U ( 'Suppliers/index' ) );
	}
	/**
	 * 更新后更新缓存
	 */
	function _after_update() {
		parent::_cache ( 1, 'suppliers' );
	}
}